package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils3.date.IceDateFormatUtils;
import cc.edt.iceutils3.date.IceDateOperationUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.dao.RedPacketOrderDao;
import com.edt.ichannel.api.entiy.local.RedPacketOrder;
import com.edt.ichannel.api.service.RedPacketOrderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * RedPacketOrder服务接口实现类
 * @author sl
 * @date 2018-06-26
 */
@Service
public class RedPacketOrderServiceImpl implements RedPacketOrderService {

    @Resource
    private RedPacketOrderDao redpacketorderDao;
    private Logger logger = LogManager.getLogger(RedPacketOrderServiceImpl.class);


    @Override
    public List<RedPacketOrder> selectRedPackageOrder(int day) {
        String dateStr = IceDateFormatUtils.dateToString(IceDateOperationUtils.addDays(new Date(),day),"yyyy-MM-dd");
        return redpacketorderDao.selectByDataStr(dateStr);
    }

    @Override
    public void insertList(List<RedPacketOrder> redPacketOrderList) {
        if (redPacketOrderList != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < redPacketOrderList.size();){
                if(lastIndex > redPacketOrderList.size()){
                    lastIndex = redPacketOrderList.size();
                    redpacketorderDao.insertList(redPacketOrderList.subList(index,lastIndex));
                    break;
                }else{
                    redpacketorderDao.insertList(redPacketOrderList.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+redPacketOrderList.size());
        }else{
            logger.info("list为空");
        }
    }
}